package com.dameng.system.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dameng.system.entity.SystemUser;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

@Mapper
public interface SystemUserMapper  extends BaseMapper<SystemUser> {

    List<SystemUser> selectUserList(Page<SystemUser> page, SystemUser systemUser);

    /**
     * 该方法等同于getUserAndDept和getDeptById关联查询
     * @param page
     * @param systemUser
     * @return
     */
    @Select("<script>" +
            "select u.*, d.name deptName from system_user u LEFT JOIN system_dept d ON u.dept_id = d.dept_id where 1=1 " +
            "<if test=\"systemUser.deptId != null and '' != systemUser.deptId\"> and u.dept_id = #{systemUser.deptId} </if>" +
            "<if test=\"systemUser.name != null and '' != systemUser.name\"> and u.name LIKE '%${systemUser.name}%' </if>" +
            "</script>")
    Page<SystemUser> getUserList(Page<SystemUser> page, @Param("systemUser") SystemUser systemUser);

    @Results({
            @Result(id=true,column="userId",property="user_id"),
            @Result(column="username",property="username"),
            @Result(column="name",property="name"),
            @Result(column="pwd",property="pwd"),
            @Result(column="deptId",property="dept_id"),
            @Result(column="status",property="status"),
            @Result(column="dept_id",property="systemDept", one=@One(select="com.dameng.system.mapper.SystemDeptMapper.selectById",fetchType= FetchType.DEFAULT))
    })
    @Select("select * from system_user ${ew.customSqlSegment}")
    SystemUser getUserAndDept(@Param(Constants.WRAPPER) QueryWrapper<SystemUser> wrapper);

}
